package com.applet.system.controller;

import com.applet.code.util.Page;
import com.applet.system.entity.HttpResult;

import com.applet.system.utils.Md5Utils;
//import UserContextUtil;

import com.applet.system.utils.UserContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.applet.system.entity.User;
import com.applet.system.entity.dto.UserDto;
import com.applet.system.service.UserService;

/**
 * 
 * <b>Description:用户管理/用户角色管理</b> 
 * 
 * <b>Author:</b> 1217460714@qq.com
 * <b>Date:</b> 2019年11月21日 上午9:38:01
 * <b>Copyright:</b> Copyright &copy;2018-2019 istp Co., Ltd. All rights reserved.
 * <b>Changelog:</b>
 *   Ver   Date                  Author              Detail
 *   ----------------------------------------------------------------------
 *   1.0   2019年11月21日 上午9:38:01   1217460714@qq.com
 *         new file.
 * </pre>
 */
@RestController
@RequestMapping("/system/user")
public class UserController {
	
	@Value("${console.user.initialPwd}")
	private String initialPwd;

	@Value("${console.user.initialHeadImg}")
	private String initialHeadImg;

	@Autowired	
	private UserService userServiceImpl;

	@GetMapping("/getPage")
	public HttpResult getPage(Page page, UserDto user) {
		page.initPagingData(UserDto.class);
		return new HttpResult(page.setResult(this.userServiceImpl.getPage(user)));
	}
	
	@PostMapping("/enable")
	public HttpResult enable(User user) {
		user.setEditor(UserContextUtil.get().getUserName());
		user.setIsEnable(true);
		this.userServiceImpl.updateUserStatus(user);
		return HttpResult.OK;
	}

	/**
	 * 禁用
	 * @param id
	 * @return
	 */
	@PostMapping("/prohibit")
	public HttpResult prohibit(User user) {
		user.setEditor(UserContextUtil.get().getUserName());
		user.setIsEnable(false);
		this.userServiceImpl.updateUserStatus(user);
		return HttpResult.OK;
	}
	
	/**
	 * 重置密码
	 * @param id
	 * @return
	 */
	@PostMapping("/resetPwd")
	public HttpResult resetPwd(User user) {
		user.setEditor(UserContextUtil.get().getUserName());
		user.setPassword(Md5Utils.encode(this.initialPwd).toLowerCase());
		user.setEditor(UserContextUtil.get().getUserName());
		this.userServiceImpl.resetPwd(user);
		return HttpResult.OK;
	}
	
	/**
	 * 新增用户
	 * @param id
	 * @return
	 */
	@PostMapping("/insert")
	public HttpResult insert(User user) {
		if (user.getPassword() == null) {
			user.setPassword(Md5Utils.encode(this.initialPwd).toLowerCase());
		}
		String userName = UserContextUtil.get().getUserName();
		user.setCreater(userName);
		user.setEditor(userName);
		user.setHeadImg(this.initialHeadImg);
		this.userServiceImpl.insert(user);
		return HttpResult.OK;
	}

	/**
	 * 配置用户角色
	 * @param id
	 * @return
	 */
	@PostMapping("/settingRoles")
	public HttpResult settingRoles(@RequestBody UserDto user) {
		user.setCreater(UserContextUtil.get().getUserName());
		this.userServiceImpl.settingRoles(user);
		return HttpResult.OK;
	}
	
	/**
	 * 获取用户角色id集合
	 * @param id
	 * @return
	 */
	@GetMapping("/getUserRoleIds")
	public HttpResult getUserRoleIds(Long id) {
		return new HttpResult(this.userServiceImpl.getUserRoleIds(id));
	}
	
	/**
	 * 获取初始密码
	 * @return
	 */
	@GetMapping("/getInitialPwd")
	public HttpResult getInitialPwd() {
		return new HttpResult(this.initialPwd);
	}
}
